
package com.kanjian.music.activity;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.kanjian.music.R;
import com.kanjian.music.adapter.MusicListListAdapater;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.constants.MusicListTypeConstants;
import com.kanjian.music.dialog.MusicMenuDialog;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.entity.RankDetail;
import com.kanjian.music.network.HttpLoader;
import com.kanjian.music.service.PlayerService;
import com.kanjian.music.util.PlayerUtil;

/**
 * 歌单列表页面
 * 
 * @author xuyanjun
 */
public class MusicListActivity extends BaseActivity implements LoaderCallbacks<String> {

    private ListView mListView;

    private MusicListListAdapater mListAdapter;

    private int mMusicListType;

    private String mRequestType;

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_music_list);
        mListView = (ListView) findViewById(R.id.musiclist_listview);
        mMusicListType = getIntent().getIntExtra(IntentConstants.MUSICLISTTYPE,
                MusicListTypeConstants.MUSIC_LIST_TYPE_RECOMMEND);
        mListAdapter = new MusicListListAdapater(this, mListView);
        mListView.setAdapter(mListAdapter);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Music music = mListAdapter.getItem(position);
//                PlayerUtil.addMusic(music);
//                mPlayService.play();
                MusicMenuDialog.newInstance(music).show(getSupportFragmentManager(), "");
            }
        });
        super.onCreate(arg0);
        setTitle();
        loadData();
    }

    private void loadData() {
        Bundle args = new Bundle();
        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getRankDetailRequest(mRequestType));
        getSupportLoaderManager().initLoader(0, args, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void setTitle() {
        String title = null;
        switch (mMusicListType) {
            case MusicListTypeConstants.MUSIC_LIST_TYPE_RECOMMEND:
                title = "你可能喜欢";
                // mRequestType = RankDetail.
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_HOT:
                title = "24小时热门榜";
                mRequestType = RankDetail.TYPE_24H;
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_MONTH:
                title = "最近30天";
                mRequestType = RankDetail.TYPE_30D;
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_WEEK:
                mRequestType = RankDetail.TYPE_7D;
                title = "最近7天";
                break;
            case MusicListTypeConstants.MUSIC_LIST_TYPE_NEW:
                mRequestType = RankDetail.TYPE_NEW;
                title = "新晋榜";
                break;
            default:
                title = "24小时热门榜";
                mRequestType = RankDetail.TYPE_24H;
                break;
        }
        super.setTitle(title);
    }

    @Override
    public Loader<String> onCreateLoader(int arg0, Bundle arg1) {
        return new HttpLoader(MusicListActivity.this, arg1);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            ArrayList<Music> musicList = Music.getMusicListFromJson(data);
            ArrayList<Musician> musicianList = Musician.getMusicianListFromJson(data);
            if (musicianList != null) {
                for (int i = 0; i < musicianList.size(); i++) {
                    System.out.println(musicianList.get(i).mUserName);
                }
            }
            mListAdapter.setMusicList(musicList);
        }
    }

    @Override
    public void onLoaderReset(Loader<String> arg0) {
        System.out.println("onResetLoader");
    }

}
